<?php
header("Content-type: text/xml\n\n");
echo ( '<?xml version="1.0"?>' . "\n");
?>

<?php

session_start();

include '../datos/AccesoDatos.php';
include 'session.php';
include_once ('sqlinjection.php');
if (isset($_GET['accion'])) {
    $accion = $_GET['accion'];
}



$objFactura = new Factura();
switch ($accion) {
    case "pendientes":$objFactura->LitasPendientes();
        break;
    
    case "paquetespendientes":$objFactura->ListaPaquetesPendientes();
        break;
    
    
};

class Factura {
    
    
    
  public function LitasPendientes() {



        $centromedico = $_GET['centromedico'];
        
        $node = (integer) $_REQUEST["nodeid"];
// detect if here we post the data from allready loaded tree
// we can make here other checks
        if ($node > 0) {
            $n_lft = (integer) $_REQUEST["n_left"];
            $n_rgt = (integer) $_REQUEST["n_right"];
            $n_lvl = (integer) $_REQUEST["n_level"];

            $n_lvl = $n_lvl + 1;



            $sql = "pa_ListarFacturasPendientes('" . $centromedico . "','" . $_REQUEST["nodeid"] . "','2";

            $level = 1;
            $leaf = 'true';
        } else {
            // initial grid
            

            $sql = "pa_ListarFacturasPendientes('" . $centromedico . "','','1";
            $level = 0;

            $leaf = 'false';
        }


        $page = $_GET['page'];

// get how many rows we want to have into the grid - rowNum parameter in the grid 
        $limit = $_GET['rows'];

// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel 
        $sidx = $_GET['sidx'];

// sorting order - at first time sortorder 
        $sord = $_GET['sord'];


        $objDatos = new AccesoDatos();
        $objDatos->ejecutarConsulta($sql);
        $count = $objDatos->devuelveRegsLeidos();
        // calculate the total pages for the query 
        if ($count > 0 && $limit > 0) {
            $total_pages = ceil($count / $limit);
        } else {
            $total_pages = 0;
        }

// if for some reasons the requested page is greater than the total 
// set the requested page to total page 
        if ($page > $total_pages)
            $page = $total_pages;

// calculate the starting position of the rows 
        $start = $limit * $page - $limit;

// if for some reasons start position is negative set it to 0 
// typical case is that the user type 0 for the requested page 
        if ($start < 0)
            $start = 0;

        $s = '<rows>';
        $s .='<page>' . $page . '</page>';
        $s .= '<total>' . $total_pages . "</total>";
        $s .='<records>' . $count . '</records>';

        $cont = 0;

        while ($row = $objDatos->devuelveArreglo()) {

            if ($cont >= $start) {

                if ($level == 0) {
                    $s .= '<row id="' . $row['id_cita'] . '">';
                   
                    $s .= '<cell><![CDATA[no]]></cell>';
                    $s .= '<cell><![CDATA[' . $row['ds_identificacion'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['ds_nombre'] . ' ' . $row['ds_apellido1'] .' ' . $row['ds_apellido2'] .  ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['fc_fecha'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['vl_abonototal'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['vl_saldo'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['vl_precio'] . ']]></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    
                } else {
                    
                    $s .= '<row id="' .$row['ID_FACTURABONO'] . '">';
                    $s .= '<cell>'.$_REQUEST["nodeid"] .'</cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell>'.($cont + 1).' '._('abono').'</cell>';
                    $s .= '<cell><![CDATA[' . $row['FC_FECHA'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['VL_ABONO'] . ']]></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    
                }

               
                $s .= "<cell>" . $level . "</cell>";
                $s .= "<cell>" . $row[lft] . "</cell>";
                $s .= "<cell>" . $row[rgt] . "</cell>";

                $s .= "<cell>" . $leaf . "</cell>";
                $s .= "<cell>false</cell>";


                $s .= '</row>';
            }

            $cont++;
        }
        $s .='</rows>';
        echo $s;
    }

  public function ListaPaquetesPendientes() {



        $centromedico = $_GET['centromedico'];
        
        $node = (integer) $_REQUEST["nodeid"];
// detect if here we post the data from allready loaded tree
// we can make here other checks
        if ($node > 0) {
            $n_lft = (integer) $_REQUEST["n_left"];
            $n_rgt = (integer) $_REQUEST["n_right"];
            $n_lvl = (integer) $_REQUEST["n_level"];

            $n_lvl = $n_lvl + 1;



            $sql = "pa_ListarFacturasPaquetesPendientes('" . $centromedico . "','" . $_REQUEST["nodeid"] . "','2";

            $level = 1;
            $leaf = 'true';
        } else {
            // initial grid
            

            $sql = "pa_ListarFacturasPaquetesPendientes('" . $centromedico . "','','1";
            $level = 0;

            $leaf = 'false';
        }


        $page = $_GET['page'];

// get how many rows we want to have into the grid - rowNum parameter in the grid 
        $limit = $_GET['rows'];

// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel 
        $sidx = $_GET['sidx'];

// sorting order - at first time sortorder 
        $sord = $_GET['sord'];


        $objDatos = new AccesoDatos();
        $objDatos->ejecutarConsulta($sql);
        $count = $objDatos->devuelveRegsLeidos();
        // calculate the total pages for the query 
        if ($count > 0 && $limit > 0) {
            $total_pages = ceil($count / $limit);
        } else {
            $total_pages = 0;
        }

// if for some reasons the requested page is greater than the total 
// set the requested page to total page 
        if ($page > $total_pages)
            $page = $total_pages;

// calculate the starting position of the rows 
        $start = $limit * $page - $limit;

// if for some reasons start position is negative set it to 0 
// typical case is that the user type 0 for the requested page 
        if ($start < 0)
            $start = 0;

        $s = '<rows>';
        $s .='<page>' . $page . '</page>';
        $s .= '<total>' . $total_pages . "</total>";
        $s .='<records>' . $count . '</records>';

        $cont = 0;

        while ($row = $objDatos->devuelveArreglo()) {

            if ($cont >= $start) {

                if ($level == 0) {
                    $s .= '<row id="' . $row['id_citapaquete'] . '">';
                   
                    $s .= '<cell><![CDATA[no]]></cell>';
                    $s .= '<cell><![CDATA[' . $row['ds_identificacion'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['ds_nombre'] . ' ' . $row['ds_apellido1'] .' ' . $row['ds_apellido2'] .  ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['ds_paquete'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['fc_fecha'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['vl_abonototal'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['vl_saldo'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['vl_precio'] . ']]></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    
                } else {
                    
                    $s .= '<row id="' .$row['ID_FACTURAPAQUETEABONO'] . '">';
                    $s .= '<cell>'.$_REQUEST["nodeid"] .'</cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell>'.($cont + 1).' '._('abono').'</cell>';
                    $s .= '<cell><![CDATA[' . $row['FC_FECHA'] . ']]></cell>';
                    $s .= '<cell><![CDATA[' . $row['VL_ABONO'] . ']]></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    $s .= '<cell></cell>';
                    
                }

               
                $s .= "<cell>" . $level . "</cell>";
                $s .= "<cell>" . $row[lft] . "</cell>";
                $s .= "<cell>" . $row[rgt] . "</cell>";

                $s .= "<cell>" . $leaf . "</cell>";
                $s .= "<cell>false</cell>";


                $s .= '</row>';
            }

            $cont++;
        }
        $s .='</rows>';
        echo $s;
    }
    
}